package com.just521.xinshoutong.listadapter;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.InflateException;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.just521.xinshoutong.R;
import com.just521.xinshoutong.http.ImageDownLoader;
import com.just521.xinshoutong.json.MainList_SummaryRec;


public class MainNoteListAdapter<T> extends ListAdapter_base
{
	class ViewHolder{
		TextView alpha;
		TextView content;
		TextView time;
		CheckBox status;
		ImageView picflag;
		ImageView audioflag;
		ImageView touchflag;
	}
	
	private ImageDownLoader asyncImageLoader;
	private	ListView		mList=null;
	long mcurweekbeg;
	long mlastweekbeg;
	long mtodaytime;
	Date mcurday;
	Date mlastday;
	Date magoday;
	Map<String,Integer> alphas=new HashMap<String,Integer>();
	final static String curweek="本周";
	final static String lastweek="上周";
	final static String agoweek="更早";
	
	final static String today="今天";
	final static String yestoday="昨天";
	final static String agoyestoday="前天";
	public MainNoteListAdapter(Context c,List<T> list, ListView lv )
	{	
		super(c,list);
		mList = lv;
		asyncImageLoader = new ImageDownLoader();
		getWeekBegin();
		Calendar cal=Calendar.getInstance();
		mcurday=cal.getTime();
		cal.add(Calendar.DATE, -1);
		mlastday=cal.getTime();
		cal.add(Calendar.DATE, -1);
		magoday=cal.getTime();
		
	}
	
	private long getWeekBegin(){
		long ret=0;
		Calendar c=Calendar.getInstance();
		int dayofweek=c.get(Calendar.DAY_OF_WEEK)-1;
		int weekDay = dayofweek==0?7:dayofweek;  
		c.add(Calendar.DATE,1 - weekDay);
		Date curbeg=c.getTime();
		curbeg.setHours(0);
		curbeg.setMinutes(0);
		mcurweekbeg=curbeg.getTime();
		
		c.add(Calendar.DATE,-7);
		Date lastbeg=c.getTime();
		lastbeg.setHours(0);
		lastbeg.setMinutes(0);
		mlastweekbeg=lastbeg.getTime();
		
		return ret;
	}
	protected View initView(ViewGroup parent){
		View retV=null;
		try{
			retV = mfactory.inflate(R.layout.main_notelist_item,parent, false);
			ViewHolder temp = new ViewHolder();
			temp.content=(TextView)retV.findViewById(R.id.content);
			temp.alpha=(TextView)retV.findViewById(R.id.alpha);
			temp.time=(TextView)retV.findViewById(R.id.time);
//			temp.picflag = (ImageView)retV.findViewById(R.id.pic);
//			temp.audioflag = (ImageView)retV.findViewById(R.id.audio);
//			temp.touchflag = (ImageView)retV.findViewById(R.id.touch);
			temp.status = (CheckBox)retV.findViewById(R.id.status);
			retV.setTag(temp);
		}catch(InflateException e){
		}
		return retV;
	}
	
	protected void setContent(View retV,Object content,int pos){
		
		ViewHolder temp = (ViewHolder)retV.getTag();
		MainList_SummaryRec rec=(MainList_SummaryRec)content;
		temp.content.setText(rec.getSummary());
        String timestr=null;
        long notecreatetime=rec.getCreatetime();
        Date d=new Date(notecreatetime);
        if(d.getDate()==mcurday.getDate() && d.getMonth()==mcurday.getMonth()){
        	
        	temp.time.setText(today);
        }else if(d.getDate()==mlastday.getDate() && d.getMonth()==mlastday.getMonth()){
        	
        	temp.time.setText(yestoday);
        }
        else if(d.getDate()==magoday.getDate() && d.getMonth()==magoday.getMonth()){
        	
        	temp.time.setText(agoyestoday);
        }else{
        	
        	timestr=String.valueOf(d.getMonth()+1)+"/"+String.valueOf(d.getDate());
        	temp.time.setText(timestr);
        }
        
		if(rec.getCreatetime()>mcurweekbeg){//cur week
			if(alphas.get(curweek)==null){
				temp.alpha.setVisibility(View.VISIBLE);
				temp.alpha.setText(curweek);
				alphas.put(curweek, pos);
			}else if(pos==alphas.get(curweek)){
				temp.alpha.setVisibility(View.VISIBLE);
				temp.alpha.setText(curweek);
			}else{
				temp.alpha.setVisibility(View.GONE);
			}
		}else if(rec.getCreatetime()>mlastweekbeg){// last week
			if(alphas.get(lastweek)==null){
				temp.alpha.setVisibility(View.VISIBLE);
				temp.alpha.setText(lastweek);
				alphas.put(lastweek, pos);
			}else if(pos==alphas.get(lastweek)){
				temp.alpha.setVisibility(View.VISIBLE);
				temp.alpha.setText(lastweek);
			}else{
				temp.alpha.setVisibility(View.GONE);
			}
		}else{// long long ago
			if(alphas.get(agoweek)==null){
				temp.alpha.setVisibility(View.VISIBLE);
				temp.alpha.setText(agoweek);
				alphas.put(agoweek, pos);
			}else if(pos==alphas.get(agoweek)){
				temp.alpha.setVisibility(View.VISIBLE);
				temp.alpha.setText(agoweek);
			}else{
				temp.alpha.setVisibility(View.GONE);
			}
		}
       
        
	}
	


}
